﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SCS_350_snmp;
using _3S_Cam;
using izmer_bizss;


namespace DeviceLibsTester
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private delegate string DynamicFunc(params object[] parameters);
        private DynamicFunc _dynamicFunc;
        //private readonly string _dllPath = "M:\\Projects\\CodePlex\\Beltelecom Monitoring .NET 4.5_WPF\\monitor_server_2_WPF\\bin\\Debug\\Resources\\Libs\\t-vips_analizator.dll";
        private readonly string _dllPath = "M:\\Projects\\CodePlex\\SCS_350\\DeviceLibsTester\\bin\\Debug\\t-vips_analizator.dll";
        private readonly string _dllName = "t-vips_analizator.dll";
        private bool _initialized;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Initialize();
            //var ret = Do("192.168.2.249", "M:\\Новоселье_11.27 13.40_TNS547.csv");
            //var ret = TryScs();
            TryCam();
            TrySome();
        }

        void TrySome()
        {
            Browser.Navigate("http://192.168.0.102");
        }

        void TryCam()
        {
            var cc = new CamController("192.168.0.102");
            var win = new MainWindow();
            win.Content = cc.GetControl();
            win.Show();
        }

        string TryScs()
        {
            return Scs350Worker.GetStatus("10.10.55.110");
        }

        string Do(string ip, string path)
        {
            if (!_initialized)
            {
                var check = Initialize();
                if (check != "ok")
                {
                    return check;// "err:" + Server.Rm.GetString("DllNotInitialized") + ".\r\n" + _dllName;
                }
            }
            string[] parsString = { ip, path };
            var args = new object[2];
            args[0] = "getlog";
            args[1] = parsString;
            try
            {
                return _dynamicFunc(args);
            }
            catch (Exception ex)
            {
                if (ex is TargetInvocationException && ex.InnerException != null)
                    return "err:" + ex.InnerException.Message + ": " + ip;
                return "err:" + ex.Message + ": " + ip;
            }
        }

        private static DynamicFunc MakeDynamicFunc(object target, MethodInfo method)
        {
            return par => (string)method.Invoke(target, par);
        }

        public string Initialize()
        {
            if (!File.Exists(_dllPath))
            {
                if (string.IsNullOrEmpty(_dllName))
                    return "err:" + "DllIsNotExist";
                return "err:" + "DllIsNotExist"+ ": " + _dllName;
            }
            try
            {
                Assembly assemblyDll = Assembly.LoadFile(_dllPath);
                Type[] typeslist = assemblyDll.GetExportedTypes();
                MethodInfo[] methods = typeslist[0].GetMethods();
                var callMethod = methods.First(met => met.Name.Contains("CallMethod"));
                var callClass = Activator.CreateInstance(typeslist[0]);
                _dynamicFunc = MakeDynamicFunc(callClass, callMethod);
            }
            catch (Exception ex)
            {
                return "err:" + ex.Message + "\r\n" + _dllName;
            }
            _initialized = true;
            return "ok";
        }

    }
}
